package com.huaao.micropolice.core.service.system.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.druid.util.StringUtils;
import com.huaao.micropolice.core.common.page.GridPage;
import com.huaao.micropolice.core.common.page.Page;
import com.huaao.micropolice.core.common.page.Parameter;
import com.huaao.micropolice.core.common.page.Sort;
import com.huaao.micropolice.core.model.system.Dict;
import com.huaao.micropolice.core.repository.system.DictDao;
import com.huaao.micropolice.core.service.system.DictService;

@Service
@Transactional
public class DictServiceImpl implements DictService{

	@Autowired
	private DictDao dao;
	
	@Override
	public Page queryDictLike(String name,GridPage gridPage) {
		Page page = new Page();
		Sort sort = new Sort();
		if(gridPage != null){
			page.setPageNum(gridPage.getPage());
			page.setPageSize(gridPage.getRows());
			if(!StringUtils.isEmpty(gridPage.getSidx()) ){
				sort.setName(gridPage.getSidx());
				sort.setType(gridPage.getSord());
			}
		}
		
		List<Parameter> params = new ArrayList<Parameter>();
		if(!StringUtils.isEmpty(name)){
			params.add(new Parameter("name_cn_"+name));
		}
		params.add(new Parameter("parentId_is_null"));
 		dao.findByPage(Dict.class, params, sort, page);
 		return page;
	}

	@Override
	public List<Dict> queryDictType(String dictId) {
		// TODO Auto-generated method stub
		
		return dao.queryDictType(dictId);
	}

	@Override
	public String save(Dict obj) {
		// TODO Auto-generated method stub
		return dao.save(obj);
	}

	@Override
	public void update(Dict obj) {
		// TODO Auto-generated method stub
		dao.update(obj);
	}

	@Override
	public void del(String id) {
		// TODO Auto-generated method stub
		dao.deleteById(Dict.class, id);
	}
	
}
